package GUI;

import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import Client.BusinessImplements;
import Controler.Add_new_Author_controler;
import Controler.Add_new_Subject_controler;
import javax.swing.border.LineBorder;
/**
 * @author Lucie
 * Class name Add_NewSubject_panel
 * extend  Abstract_Jpanel
 * purpose: this panel get a subject name from the user and call the controller 
 * to add new subject to the data base
 */
public class Add_NewSubject_panel extends Abstract_Jpanel {

	private JTextField textFieldname;

	/**
	 * Create the panel.
	 */
	public  Add_NewSubject_panel() {
		setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(SystemColor.activeCaption, 6));
		panel.setBackground(SystemColor.inactiveCaption);
		panel.setBounds(23, 59, 575, 214);
		add(panel);
		panel.setLayout(null);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new LineBorder(SystemColor.control, 6));
		panel_2.setBackground(new Color(220, 220, 220));
		panel_2.setBounds(66, 37, 415, 116);
		panel.add(panel_2);
		panel_2.setLayout(null);
		
		JLabel lblSubjectName = new JLabel("subject name:");
		lblSubjectName.setBounds(49, 41, 97, 26);
		panel_2.add(lblSubjectName);
		lblSubjectName.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		textFieldname = new JTextField();
		textFieldname.setBounds(171, 46, 178, 20);
		panel_2.add(textFieldname);
		textFieldname.setColumns(10);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBounds(23, 284, 585, 55);
		add(panel_1);
		panel_1.setLayout(null);
		//btnBack ;When you click this button you will return to 
		//sing in panel screen
		JButton btnBack = new JButton("back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				Sign_in_panel sip=new Sign_in_panel();
				on_screen_unload();
				BusinessImplements.GetInstance().getMainFrame().setContentPane(sip);
				sip.setSize(getMaximumSize());
				sip.on_screen_load();
				
				
			}
		});
		
		btnBack.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnBack.setBounds(10, 11, 89, 36);
		panel_1.add(btnBack);
		/*
		 * btnAdd=When you click this button you will call get_name()
		 * a method of the controller of this panel if this method return true we will
		 * call the methods of this class next_screen_success(),empty_text_fields();
		 * else we call next_screen_faild(add_author_controler.get_msg());	
		 * 	add_author_controler.get_msg(); return the msg from the controller with all
		 * the error msg.
		 */
		JButton btnAdd = new JButton("add");
		btnAdd.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Add_new_Subject_controler add_author_controler=new Add_new_Subject_controler();
				
				if(add_author_controler.get_data(get_name()))
				{
					next_screen_success();
					empty_text_fields();
					
				}
				else {
					next_screen_faild(add_author_controler.get_msg());
					
				}
				
				
				
			}
		});
		btnAdd.setBounds(469, 11, 89, 36);
		panel_1.add(btnAdd);
		
		JLabel lblAddNewAuthor = new JLabel("Add New Subject");
		lblAddNewAuthor.setFont(new Font("MV Boli", Font.BOLD, 30));
		lblAddNewAuthor.setForeground(new Color(25, 25, 112));
		lblAddNewAuthor.setBounds(38, 11, 338, 37);
		add(lblAddNewAuthor);

	}

	@Override
	public void next_screen() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void prev_screen() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void on_screen_load() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void on_screen_unload() {
		// TODO Auto-generated method stub
		
	}
	public String get_name()
	{
		return textFieldname.getText();
	}
	
	/**
	 * this method display showMessageDialog with a success msg
	 */
	public void next_screen_success()
	{
		JOptionPane.showMessageDialog(this,"apply success");
	}
	/**
	 *  this method display showMessageDialog with a failed msg
	 * @param msg
	 */
	public void next_screen_faild(String msg)
	{
		JOptionPane.showMessageDialog(this,"apply faild "+msg);
	}
	/**
	 * this method empty all the text field
	 */
	public void empty_text_fields()
	{
		textFieldname.setText(null);
		
	}
	
	
}
